﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Day
{
    public class AccountDayTradeInstrumentAnalysis
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }
        /// <summary>
        /// 交易合约
        /// </summary>
        public int InstrumentId { get; set; }
        /// <summary>
        /// 交易合约
        /// </summary>
        public virtual Instrument Instrument { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }

        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }


        ///// <summary>
        ///// 删除某日的所有分析数据
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = from t in db.AccountDayTradeInstrumentAnalysis where t.TradeHistoryId == tradeHistoryId select t;
        //    foreach (AccountDayTradeInstrumentAnalysis d in query)
        //    {
        //        db.AccountDayTradeInstrumentAnalysis.Remove(d);
        //    }
        //    db.SaveChanges();
        //}


        ///// <summary>
        ///// 保存账号日分析情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitAccountInstrumentDayAnalysis(int tradeHistoryId, string pdate, DB.DBManager db)
        //{
        //    List<Data.ServerData.T_Account> serverAccounts = new Data.ServerData.T_Account().GetList();
        //    foreach (Data.ServerData.T_Account account in serverAccounts)
        //    {
        //        Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
        //        List<string> instruments = tth.GetAccountDayInstrument(pdate, account.Id);
        //        foreach (string instrument in instruments)
        //        {
        //            #region i为0是多，i为1是空
        //            for (int i = 0; i < 2; i++)
        //            {
        //                Data.Day.AccountDayTradeInstrumentAnalysis change = new Data.Day.AccountDayTradeInstrumentAnalysis();
        //                Sys.Account _account = new Sys.Account();
        //                change.AccountId = _account.GetAccountId(account.LoginAccount);
        //                change.TradeHistoryId = tradeHistoryId;
        //                change.Direction = i;
        //                change.InstrumentId = new Sys.Instrument().GetInstrumentId(instrument, db);
        //                change.PingJinPing = tth.GetDayInstrumentPingCount(pdate, account.Id, instrument, change.Direction.ToString(), db);
        //                change.MaxReturn = tth.GetInstrumentMaxReturn(pdate, account.Id, instrument, change.Direction.ToString(), db);
        //                change.AvgPostionTime = -1;
        //                change.OverStopLoss = new DayWinOrLoseTick().GetInstrumentOverStopLoss(pdate, account.Id, Config.DefaultValue.OverLostTick, change.InstrumentId, change.Direction, db);
        //                db.AccountDayTradeInstrumentAnalysis.Add(change);
        //            }
        //            #endregion
        //        }
        //    }
        //    db.SaveChanges();
        //}


        //public List<AccountDayTradeInstrumentAnalysis> GetAccountDayTradeInstrumentAnalysis(int accountid, int tradeHistoryId, DB.DBManager db)
        //{
        //    List<AccountDayTradeInstrumentAnalysis> instruments = new List<AccountDayTradeInstrumentAnalysis>();
        //    var query = db.AccountDayTradeInstrumentAnalysis.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
        //    if (query.Count() > 0)
        //    {
        //        instruments = query.ToList();
        //    }
        //    return instruments;
        //}
    }
}